package com.sc.service.impl.csm;

import com.sc.dto.PatrolBusinessDTO;
import com.sc.mapper.csm.PatrolBusinessMapper;
import com.sc.result.PageResult;
import com.sc.service.csm.PatrolBusinessService;
import com.sc.vo.PatrolBusinessVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class PatrolBusinessServiceImpl implements PatrolBusinessService {

    private final PatrolBusinessMapper baseMapper;

    @Override
    public PageResult<PatrolBusinessVO> statistics(PatrolBusinessDTO patrolBusinessDTO) {
        int start = (patrolBusinessDTO.getPageNum() - 1) * patrolBusinessDTO.getPageSize();
        int end = patrolBusinessDTO.getPageNum() * patrolBusinessDTO.getPageSize();

        List<PatrolBusinessVO> list = baseMapper.statistics(patrolBusinessDTO);
        list.forEach(patrolBusinessVO -> {
            if (patrolBusinessVO.getOnTimeComplete() + patrolBusinessVO.getTimeoutComplete() + patrolBusinessVO.getTimeoutNotComplete() == 0) {
                patrolBusinessVO.setCompleteRate("无已完成任务");
            } else {
                int rate = (patrolBusinessVO.getOnTimeComplete() + patrolBusinessVO.getTimeoutComplete()) * 1000 / (patrolBusinessVO.getOnTimeComplete() + patrolBusinessVO.getTimeoutComplete() + patrolBusinessVO.getTimeoutNotComplete());
                patrolBusinessVO.setCompleteRate(rate / 10 + "." + rate % 10 + "%");
            }
        });

        PageResult<PatrolBusinessVO> page = new PageResult<>(list.size(), list.subList(start, Math.min(list.size(), end)));
        return page;
    }
}
